//
//  BDMyOrderLayout.m
//  iOS_beboy
//
//  Created by wen on 2017/9/19.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDMyOrderLayout.h"
#import "BDPostModel.h"
#import "BDMyOrderCouponModel.h"
@implementation BDMyOrderLayout

- (instancetype)initWithOrder:(BDMyOrderModel *)order{
    if (!order) return nil;
    if (self = [super init]) {
        _order = order;
        [self layout];
    }
    return self;
}

- (void)layout{
    
    [self _layoutTitle];
    [self _layoutContent];
    [self _layoutToolbar];
    
    _marginTop = 8;
    _marginBottom = 2;
    _height = 0;
    _height += _titleHeight;
    _height += _toolHeight;
    _height += (kBDOrderCellContentImageHeight + kBDOrderCellContentImagePadding * 2);
    _height += _marginBottom;
    _height += _marginTop;
}

- (void)_layoutTitle{
    NSMutableAttributedString *text = [NSMutableAttributedString new];
    if (_order.type == BDMyOrderTypePost) {
        NSMutableAttributedString *subString = [NSMutableAttributedString attachmentStringWithEmojiImage:[UIImage imageNamed:@"icon_zuozhe"] fontSize:15];
        [text appendAttributedString:subString];
        NSString *userName = _order.object[@"user"][@"nickname"];
        if (![userName isNotBlank]) {
            userName = ASLocalizedString(@"该用户不存在");
        }
        NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@", userName]];
        titleString.font = [UIFont systemFontOfSize:15];
        titleString.color = [UIColor colorWithHexString:@"#111111"];
        [text appendAttributedString:titleString];
    }else if (_order.type == BDMyOrderTypeShopCoupon) {
        NSMutableAttributedString *subString = [NSMutableAttributedString attachmentStringWithEmojiImage:[UIImage imageNamed:@"dianpu"] fontSize:15];
        [text appendAttributedString:subString];
        NSString *shopName = nil;
        NSDictionary *shopInfo = _order.object[@"shop"];
        if ([shopInfo isKindOfClass:[NSDictionary class]]) {
            shopName = shopInfo[@"title"];
        }
        if ([shopName isNotBlank] == NO) {
            shopName = ASLocalizedString(@"该店不存在");
        }
        NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@", shopName]];
        titleString.font = [UIFont systemFontOfSize:15];
        titleString.color = [UIColor colorWithHexString:@"#111111"];
        [text appendAttributedString:titleString];
    }else if (_order.type == BDMyOrderTypeProduct) {
        NSMutableAttributedString *subString = [NSMutableAttributedString attachmentStringWithEmojiImage:[UIImage imageNamed:@"dianpu"] fontSize:15];
        [text appendAttributedString:subString];
        NSString *shopName = ASLocalizedString(@"夜商城");
        NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@", shopName]];
        titleString.font = [UIFont systemFontOfSize:15];
        titleString.color = [UIColor colorWithHexString:@"#111111"];
        [text appendAttributedString:titleString];
    }else if (_order.type == BDMyOrderTypeTransferToShop) {
        NSMutableAttributedString *subString = [NSMutableAttributedString attachmentStringWithEmojiImage:[UIImage imageNamed:@"dianpu"] fontSize:15];
        [text appendAttributedString:subString];
        NSString *shopName = _order.object[@"shop"][@"title"];
        if ([shopName isNotBlank] == NO) {
            shopName = ASLocalizedString(@"该店不存在");
        }
        NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@", shopName]];
        titleString.font = [UIFont systemFontOfSize:15];
        titleString.color = [UIColor colorWithHexString:@"#111111"];
        [text appendAttributedString:titleString];
    }
    
    YYTextContainer *container = [YYTextContainer new];
    container.size = CGSizeMake(kBDOrderCellContentTextWidth, CGFLOAT_MAX);
    container.maximumNumberOfRows = 1;
    
    _titleLayout = [YYTextLayout layoutWithContainer:container text:text];
    _titleHeight = 35;
}

- (void)_layoutContent{
    
    NSMutableAttributedString *titleText = nil;
    
    NSMutableAttributedString *priceText = [NSMutableAttributedString new];
    NSMutableAttributedString *renminbi = [[NSMutableAttributedString alloc] initWithString:@"¥ "];
    renminbi.color = [UIColor colorWithHexString:@"#ff4f00"];
    renminbi.font = [UIFont systemFontOfSize:12];
    [priceText appendAttributedString:renminbi];
    
    NSMutableAttributedString *moneyText = [[NSMutableAttributedString alloc] initWithString:_order.receipt_amount];
    moneyText.font = [UIFont systemFontOfSize:16];
    moneyText.color = [UIColor colorWithHexString:@"#ff4f00"];
    [priceText appendAttributedString:moneyText];
    
    switch (_order.type) {
        case BDMyOrderTypePost:{
            NSArray *imageArray = _order.object[@"images"];
            if ([imageArray isKindOfClass:[NSArray class]] && imageArray.count) {
                _contentImage = imageArray.firstObject;
            }
            
            NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
            //获取app中所有icon名字数组
            NSArray *iconsArr = infoDict[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"];
            //取最后一个icon的名字
            NSString *iconLastName = [iconsArr lastObject];
            _contentImagePlaceholder = iconLastName;
            
            NSString *post = _order.object[@"post"];
            if (![post isNotBlank]) {
                post = ASLocalizedString(@"帖子错误，无法查看");
            }
            
            titleText = [[NSMutableAttributedString alloc] initWithString:post];
            titleText.font = [UIFont systemFontOfSize:15];
            titleText.color = [UIColor colorWithHexString:@"#111111"];
            
            _buttonTitle = ASLocalizedString(@"阅读");
            _buttonBKColor = KBDColorBlue;
            _buttonTitleColor = [UIColor whiteColor];
            _buttonCanClick = YES;
        }break;
        case BDMyOrderTypeProduct:{
            _contentImage = _order.object[@"cover"];
            _contentImagePlaceholder = @"shop_pic";
            
            NSString *productName = _order.object[@"title"];
            if ([productName isNotBlank] == NO) {
                productName = ASLocalizedString(@"该商品不存在");
            }
            
            titleText = [[NSMutableAttributedString alloc] initWithString:productName];
            titleText.font = [UIFont systemFontOfSize:15];
            titleText.color = [UIColor colorWithHexString:@"#111111"];
            
            NSInteger shopping_status = [_order.object[@"shipping_status"] integerValue];
            
            switch (shopping_status) {
                case 0:
                    _buttonTitle = ASLocalizedString(@"待发货");
                break;
                case 1:
                    _buttonTitle = ASLocalizedString(@"待收货");
                    break;
                case 2:
                    _buttonTitle = ASLocalizedString(@"已收货");
                break;
                case 3:
                    _buttonTitle = ASLocalizedString(@"已退货");
                break;
                case 4:
                    _buttonTitle = ASLocalizedString(@"不可退货");
                break;
                default:
                break;
            }
            _buttonBKColor = [UIColor whiteColor];
            _buttonTitleColor = KBDColorBlue;
            _buttonCanClick = NO;
        }break;
        case BDMyOrderTypeShopCoupon:{
            _contentImage = _order.object[@"cover"];
            _contentImagePlaceholder = @"shop_pic";
            
            NSString *productName = nil;
            if (_order.object) {
                productName = _order.object[@"title"];
            }
            if ([productName isNotBlank] == NO) {
                productName = ASLocalizedString(@"该套餐不存在");
            }
            
            titleText = [[NSMutableAttributedString alloc] initWithString:productName];
            titleText.font = [UIFont systemFontOfSize:15];
            titleText.color = [UIColor colorWithHexString:@"#111111"];
            
            switch (_order.status) {
                case BDMyOrderStatusWaitPay:
                    _buttonTitle = ASLocalizedString(@"付款");
                    _buttonBKColor = [UIColor colorWithHexString:@"ff992b"];
                    _buttonTitleColor = [UIColor whiteColor];
                    _buttonCanClick = YES;
                    break;
                case BDMyOrderStatusPayBack:
                    titleText.color = [UIColor colorWithHexString:@"#e5e5e5"];
                    priceText.color = [UIColor colorWithHexString:@"#e5e5e5"];
                    _buttonTitle = ASLocalizedString(@"已退款");
                    _buttonBKColor = [UIColor whiteColor];
                    _buttonTitleColor = KBDColorBlue;
                    _buttonCanClick = NO;
                    break;
                case BDMyOrderStatusApplyRefund:
                    titleText.color = [UIColor colorWithHexString:@"#e5e5e5"];
                    priceText.color = [UIColor colorWithHexString:@"#e5e5e5"];
                    _buttonTitle = ASLocalizedString(@"退款中");
                    _buttonBKColor = [UIColor whiteColor];
                    _buttonTitleColor = KBDColorBlue;
                    _buttonCanClick = NO;
                    break;
                case BDMyOrderStatusPaySuccessed:
                    _buttonTitle = ASLocalizedString(@"查看");
                    _buttonBKColor = KBDColorBlue;
                    _buttonTitleColor = [UIColor whiteColor];
                    _buttonCanClick = YES;
                    break;
                default:
                    break;
            }
        }break;
        default:
            break;
    }
    
    YYTextContainer *container = [YYTextContainer new];
    container.size = CGSizeMake(kBDOrderCellContentTextWidth, CGFLOAT_MAX);
    container.maximumNumberOfRows = 1;
    _contentLayout = [YYTextLayout layoutWithContainer:container text:titleText];
    
    YYTextContainer *moneyContainer = [YYTextContainer new];
    moneyContainer.size = CGSizeMake(kBDOrderCellContentTextWidth, CGFLOAT_MAX);
    moneyContainer.maximumNumberOfRows = 1;
    _priceLayout = [YYTextLayout layoutWithContainer:container text:priceText];
}

- (void)_layoutToolbar{
    if ([_order isKindOfClass:[BDMyOrderModel class]]) {
        if (_order.status == BDMyOrderStatusWaitPay) {
            NSDate *end_at = [NSDate dateWithTimeIntervalSince1970:[_order.created_at floatValue] + 60 * 15];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"YYYY-mm-dd HH:mm:ss"];
            
            NSString *endString = [formatter stringFromDate:end_at];
            
            NSString *toolString = [NSString stringWithFormat:ASLocalizedString(@"请在 %@ 前完成支付"), endString];
            NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:toolString];
            text.font = [UIFont systemFontOfSize:12];
            text.color = [UIColor colorWithHexString:@"#e5e5e5"];
            
            YYTextContainer *container = [YYTextContainer new];
            container.size = CGSizeMake(kBDScreenWidth - 20, CGFLOAT_MAX);
            container.maximumNumberOfRows = 1;
            _toolLayout = [YYTextLayout layoutWithContainer:container text:text];
            _toolHeight = 35;
        }
    }
    
}

@end
